<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增行政区域管理')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-area-add">
            <div class="form-group">
                <label class="col-sm-3 control-label">区域编码：</label>
                <div class="col-sm-8">
                    <input id="areaId" name="areaId" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">区域名称：</label>
                <div class="col-sm-8">
                    <input id="areaName" name="areaName" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">区域级别：</label>
                <div class="col-sm-8">
                    <input id="areaRank" name="areaRank" class="form-control" type="text" readonly>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">上一级ID：</label>
                <div class="col-sm-8">
                    <div class="input-group">
                        <input id="treeId" name="fatherId" type="hidden" th:value="${sysArea?.areaId}"/>
                        <input class="form-control" type="text" onclick="selectAreaTree()" id="treeName" readonly="true" th:value="${sysArea?.areaName}">
                        <span class="input-group-addon"><i class="fa fa-search"></i></span>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var prefix = ctx + "system/area"
        $("#form-area-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            let areaName = $("#areaName").val();
            $.ajax ({
                url: '/system/area/listAjax',
                type: 'post',
                dataType: 'json',
                data: {"areaName": areaName},
                success: function(data) {
                    if (data.res == 'success') {
                        if ($.validate.form()) {
                            $.operate.save(prefix + "/add", $('#form-area-add').serialize());
                        }
                    } else {
                        $.modal.msgError("行政区域名称重复！");
                    }
                }
            });
        }

        /*行政区域管理-新增-选择行政区域树*/
        function selectAreaTree() {
            var options = {
                title: '行政区域管理选择',
                width: "380",
                url: prefix + "/selectAreaTree/" + $("#treeId").val(),
                callBack: doSubmit
            };
            $.modal.openOptions(options);
        }

        /*行政区域树的回调函数*/
        function doSubmit(index, layero){
            var body = layer.getChildFrame('body', index);
            $("#treeId").val(body.find('#treeId').val());
            $("#treeName").val(body.find('#treeName').val());
            $("#areaId").val(body.find('#treeId').val());
            let id = body.find('#treeId').val();
            idLength(id.length)
            layer.close(index);
        }

        /*根据父级区域编码长度决定级别*/
        function idLength(index) {
            if(index == 6){
                $("#areaRank").val(2);
            }else if (index == 8){
                $("#areaRank").val(3);
            }else if (index == 10){
                $("#areaRank").val(4);
            }else if (index == 12){
                $("#areaRank").val(4);
            }else {
                $("#areaRank").val("");
            }
        }
    </script>
</body>
</html>
